﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using LY.Entity;
using LY.Entity.Entities;
using LY.Entity.SearchEntity;
using LY.Entity.PageSearch;

namespace LY.DataAccess.Repository
{
    public class UserRepository : BaseRepository
    {
        //public VieEmployee GetEmployeeById(string userId)
        //{
        //    string sql = "select * from [VieEmployee] where EmployeeID=@EmployeeID";
        //    return base.Get<VieEmployee>(sql, new { EmployeeID = userId });
        //}
        public List<S_User> GetUpperUserList(int? id)
        {
            string sql = @" SELECT *
                       FROM [S_User] WHERE [UnitID]=@ID";
            return base.Query<S_User>(sql, new { ID = id });
        }

        public List<S_User> GetUserList(int? deptid)
        {
            string sql = @" SELECT *
                       FROM [S_User] WHERE [DeptID]=@ID";
            return base.Query<S_User>(sql, new { ID = deptid });
        }

        /// <summary>
        /// 新用户模块列表
        /// </summary>
        /// <returns></returns>
        public List<S_User_EX> QueryRoleUserList()
        {
            string sql = @" SELECT *
                       FROM [S_User] ";
            //return base.Query<S_Module>(sql, new { UserRoleID = roleid });
            return base.Query<S_User_EX>(sql, null);
        }

        /// <summary>
        /// 检索模块列表
        /// </summary>
        /// <returns></returns>
        public List<S_User_EX> QueryRoleUserList(int roleid)
        {
            string sql = @"   SELECT a.*,isnull(b.RoleID,0) as RoleID
  FROM [S_User] a LEFT OUTER     JOIN   S_RoleUser b on a.ID  = b.UserID and b.RoleID = @UserRoleID ";
            return base.Query<S_User_EX>(sql, new { UserRoleID = roleid });
            //return base.Query<S_Module>(sql, null);
        }

        public PagedList<S_User> QueryUserList(UserSearch search, PageView view)
        {

            string cols = @" [ID]
      ,[UnitID]
      ,[UserName]
      ,[LoginName]
      ,[Password]
      ,[DeptID]
      ,[DeptName]
      ,[SLDeptID]
      ,[SLDeptName]
      ,[LeaderID]
      ,[Sequence]
      ,[IsStop]
      ,[IP]
      ,[Port]
      ,[OnLineStatus]
      ,[ErrCount]
      ,[PwdUpdate]
      ,[Email] ";
            string sqlWhere = "";
            if (!string.IsNullOrEmpty(search.Name))
            {
                sqlWhere += " AND (UserName LIKE '%" + search.Name + "%' OR LoginName LIKE '%" + search.Name + "%')";
            }
            if (search.DeptID.HasValue)
            {
                sqlWhere += " AND DeptID=" + search.DeptID;
            }

            return base.PageGet<S_User>(view, cols,
                @"[S_User] "
                , sqlWhere
                , "ID DESC ",
                "");
        }

        //用户实体
        public S_User GetUser(int? id)
        {
            string sql = " SELECT * FROM S_User WHERE ID=@ID";
            return base.Get<S_User>(sql, new { ID = id });
        }

        //验证用户名是否有重复
        public int CheckUser(string loginName)
        {
            string sql = "SELECT COUNT(ID) FROM S_User WHERE LoginName=@LoginName";

            return base.Get<int>(sql, new { LoginName = loginName });
        }

        //删除用户
        public int DeleteUser(int? id)
        {
            string sql = " DELETE  FROM S_User WHERE ID=@ID";
            return base.Excute(sql, new { ID = id });
        }

        //根据当前用户的Id 查询出他的领导
        public S_User SelectUpUser(int id)
        {
            string sql = @"select * from S_User where DeptID=(
	                            select ParentID from S_Dept where ID=(
		                                select DeptID from S_User where ID=@ID)
	                    )";
            return base.Get<S_User>(sql, new { ID = id });
        }

        public int UpdatePwd(string pwd,int id) {

            string sql = @"UPDATE S_User SET PASSWORD=@PASSWORD WHERE ID=@ID";
            return (int)base.Excute(sql, new { PASSWORD =pwd,ID=id});
        }
    }
}
